package com.sentiment.database.bean;

import java.util.ArrayList;
import java.util.List;

import org.bson.Document;import org.bson.types.ObjectId;
import org.bson.types.ObjectId;

import com.sentiment.database.dbUtil.DbUtil;

public class User_Roles{
	
	public static final String dbName = "UserDB";
	public static final String collection = "User_Roles";
	
	private ObjectId _id;
	private String user_id;
	private List<String> roles_id;

	public User_Roles(String user_id,
			List<String> roles_id) {
		super();
		this.user_id = user_id;
		this.roles_id = roles_id;
	}

	public Document toDocument(){
		return new Document("user_id",user_id).append("roles_id", roles_id);
	}
	
	public Document toDocument_id(){
		return new Document("_id",_id).append("user_id",user_id).append("roles_id", roles_id);
	}
	
	public User_Roles(Document doc){
		super();
		_id = doc.getObjectId("_id");
		user_id = doc.getString("user_id");
		roles_id = (List<String>)doc.get("roles_id");
	}

	public User_Roles() {
		// TODO Auto-generated constructor stub
	}

	public String get_id() {
		return _id.toString();
	}

	public void set_id(String _id) {
		this._id = new ObjectId(_id);
	}

	public String getUser_id() {
		return user_id;
	}

	public void setUser_id(String user_id) {
		this.user_id = user_id;
	}

	public List<String> getRoles_id() {
		return roles_id;
	}

	public void setRoles_id(List<String> roles_id) {
		this.roles_id = roles_id;
	}

	@Override
	public String toString() {
		return "User_Roles [_id=" + _id + ", user_id=" + user_id
				+ ", roles_id=" + roles_id + "]";
	}
	/**
	 * 根据用户 id 获取 User_Roles
	 * @param user_id
	 * @return
	 */
	public User_Roles getUser_RolesByUser_id(String user_id) {
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		User_Roles user_roles = new User_Roles(user_id,null);
		String[] keys = {"user_id"};
		Document t = user_roles.toDocument();
		t = db.query(t, keys);
		
		db.closeConnection();
		
		if(t == null)
			return null;
		
		return new User_Roles(t);
	}
	
	/**
	 * 用于测试时向数据库中写入数据
	 */
	public void test() {
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		User user = new User();
		user = user.getUserByUserCode("user");
		
		List<String> roles_id = new ArrayList<>();
		Role role = new Role();
		role = role.getRoleByRoleName("nopay");
		roles_id.add(role.get_id());
		User_Roles user_roles = new User_Roles(user.get_id(),roles_id);
		Document t = user_roles.toDocument();
		db.insert(t);
		
		user = user.getUserByUserCode("user_pay");
		role = role.getRoleByRoleName("pay");
		roles_id.add(role.get_id());
		user_roles = new User_Roles(user.get_id(),roles_id);
		t = user_roles.toDocument();
		db.insert(t);
		
		
		user = user.getUserByUserCode("admin");
		role = role.getRoleByRoleName("admin");
		roles_id.clear();
		roles_id.add(role.get_id());		
		user_roles = new User_Roles(user.get_id(),roles_id);
		t = user_roles.toDocument();
		db.insert(t);
		
		db.closeConnection();
		
	}
	
	
	/**
	 * 更改 User_Roles 表，该方法通过 roles 重新设置 User_Roles 表中 role_id
	 * 使用前需判断 roles 存在数据库中
	 * @param roles
	 * @return
	 */
	public boolean updateRolesByRoles(List<Role> roles){
		
		DbUtil db = new DbUtil();
		
		List <String> roles_id = new ArrayList<>();
		for(int i = 0; i < roles.size(); i++){
			roles_id.add(roles.get(i).get_id());
		}
		
		User_Roles newuser_roles = this;
		newuser_roles.setRoles_id(roles_id);
						
		db.openConnection(User_Roles.dbName, User_Roles.collection);
		String[] okey = {"_id"};
		String[] nkey = {"roles_id"};
		Boolean updated = db.update(this.toDocument_id(), okey, newuser_roles.toDocument_id(), nkey);
		db.closeConnection();
		
		return updated;
	}
	
	/**
	 * 删除 User_Roles 表中所有对应角色
	 * 使用前需确认 role 存在于数据库中
	 * @param role
	 * @return
	 */
	public boolean deleteRole(Role role){
		
		DbUtil db = new DbUtil();
		db.openConnection(dbName, collection);
		
		boolean remove = db.remove("roles_id", role.get_id());
		
		db.closeConnection();
		
		return remove;
	}
	
	
	
	public static void main(String[] args) {
		
		User_Roles user_roles = new User_Roles();
//		user_roles = user_roles.getUser_RolesByUser_id("5625fd0949e1b208102ad409");
//		System.out.println(user_roles.getRoles_id().toString());
		
		
//		List<Role> roles = new ArrayList<>();
		Role role = new Role();
		role = role.getRoleByRoleId("562845e349e1b214606da99b");
//		//role = role.getRoleByRoleId("5625fd0b49e1b208102ad40f");
//		roles.add(role);
//		System.out.println(roles.get(0).getRole_detail());
		
//		user_roles.updateRolesByRoles(roles);
		
		System.out.println(user_roles.deleteRole(role));
		
		
	}
	

}
